package client;

import both.Question;
import java.awt.event.ActionEvent;
import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.xml.bind.*;
import both.*;
import server.GameShowServer;
import server.Server;

public class GameShowClient extends AbstractFileListener {

    private String serverOutputFolder;
    private String clientOutputFolder;
    private Contestant contestant;
    private long lastUpdated = 0;

    @Override
    protected void listenerTick() throws Exception {
        File qfile = new File(serverOutputFolder+"/question");
        if(qfile.exists()) {
            long currUpdate = qfile.lastModified();
            if(currUpdate>lastUpdated) {
                Question newq = getQuestion();
                ActionEvent ev = new ActionEvent(newq, 0, "new-question");
                sendEvent(ev);
                lastUpdated = currUpdate;
            }
        }
    }
    

    private Question getQuestion() throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(Question.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        File XMLfile = new File(serverOutputFolder + "/question");
        return (Question) jaxbUnmarshaller.unmarshal(XMLfile);
    }


    public static String uniqueID() throws UnknownHostException, SocketException {
        InetAddress local = InetAddress.getLocalHost();
        byte[] ip = local.getAddress();
        byte[] mac = NetworkInterface.getByInetAddress(local).getHardwareAddress();
        return bytesToHex(ip) + bytesToHex(mac);
    }


    private static String bytesToHex(byte[] in) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < in.length; i++) {
            sb.append(String.format("%02x", in[i]));
        }
        return sb.toString();
    }


    public void sendRegistration(Contestant contestant) throws UnknownHostException, SocketException, JAXBException {
        contestant.setUniqueID(uniqueID());
        this.contestant = contestant;
        JAXBContext jaxbContext = JAXBContext.newInstance(Contestant.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        File XMLfile = new File(clientOutputFolder + "/register-" + contestant.getUniqueID());
        jaxbMarshaller.marshal(contestant, XMLfile);
    }


    public void sendAnswer(Answer answer) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Answer.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            File XMLfile = new File(clientOutputFolder + "/answer-" + contestant.getUniqueID());
            jaxbMarshaller.marshal(answer, XMLfile);
            GameShowServer.tickResponses();
//          Code to advance screen, not working - Karist
//          GameShowServer.checkResponses();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    public String getServerOutputFolder() {
        return serverOutputFolder;
    }


    public void setServerOutputFolder(String serverOutputFolder) {
        this.serverOutputFolder = serverOutputFolder;
    }


    public String getClientOutputFolder() {
        return clientOutputFolder;
    }


    public void setClientOutputFolder(String clientOutputFolder) {
        this.clientOutputFolder = clientOutputFolder;
    }
}

